#环境变量说明:
#CMAKE_SOURCE_DIR  工程顶层CMakeList.txt所在的目录
#_SOURCE_DIR： 指的是工程的顶级目录。
#PROJECT_SOURCE_DIR 指的子工程CMakeList.txt所在的目录
#PROJECT_BINARY_DIR 指执行 cmake的路径

PROJECT(my_prj_name)

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)


#把Version.h.in的宏内容 转换为 cmake -DXXX 格式定义的内容
#例如：
#cmake -DSVN_VER=123 
#const char * const SVN_VER ="@SVN_VER@"; 转换为 const char * const SVN_VER ="123";
#configure_file("src/com/Version.h.in" "src/com/Version.h")

#cotire设置gcc的预编译头相关
SET (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake")
INCLUDE(cotire)

# include directories
INCLUDE_DIRECTORIES(system ./include/)
INCLUDE_DIRECTORIES(system ./include/time)
INCLUDE_DIRECTORIES(system ./src/)
INCLUDE_DIRECTORIES(after  ${PROJECT_SOURCE_DIR})

#运行cmake输出终端消息，调试可以用
message(PROJECT_SOURCE_DIR_is: ${PROJECT_SOURCE_DIR})

# lib directories
LINK_DIRECTORIES(
  #项目引用第三方库
 # ${PROJECT_SOURCE_DIR}/external/lib

  #系统目录
  /usr/local/lib
  /use/lib
)


#指定编译器
if( CMAKE_BUILD_TYPE STREQUAL "Debug" )
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -lrt -pthread -DDEBUG -Werror -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free")
    #修复 gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 环境编译不过问题用下面的内容
   # SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -g -fmax-errors=3 -Wall -Wno-error=pessimizing-move -Wno-pessimizing-move -Wno-unused-but-set-variable -Wno-unused-function -Wno-unused-variable -lrt -pthread   -Werror -DDEBUG -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -g -fmax-errors=3 -Wall -Wno-unused-but-set-variable -Wno-unused-function -Wno-unused-variable -lrt -pthread   -Werror -DDEBUG -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -rdynamic")
else( CMAKE_BUILD_TYPE STREQUAL "Release" )
    #set(CMAKE_BUILD_TYPE "Release")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O2 -lrt -DGMPERMISSION -pthread -Werror -Wno-unused-but-set-variable -Wno-unused-result -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free ")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -g -lrt -O2 -DGMPERMISSION -pthread -Werror -Wno-unused-variable -Wno-unused-result -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free")
endif( CMAKE_BUILD_TYPE STREQUAL "Debug" )

SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)


#自定义命令,根据依赖关系导出proto cpp文件 例子
##################################################
#file(GLOB_RECURSE PROTO_FILES  ${CMAKE_SOURCE_DIR} "src/proto/*.proto")
#
##对文件列表变量用 STRING(REPLACE 。。。批量改名无效的，因此写个实现
#foreach(element ${PROTO_FILES})
#	message(element=${element})
#	STRING(REPLACE ".proto" ".pb.h" h_element ${element} ) #改扩展名
#	STRING(REPLACE "/proto" "/protocpp" h_element ${h_element} ) #改路径
#	set(PROTO_CPP_FILES ${PROTO_CPP_FILES} ${h_element})
#endforeach()
#
#message(PROTO_CPP_FILES=${PROTO_CPP_FILES})
#add_custom_command(
#	OUTPUT ${PROTO_CPP_FILES}
#	DEPENDS ${PROTO_FILES}
#	COMMAND sh protoBuild.sh
#	WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} #COMMAND 执行目录
#	COMMENT "========export cpp from proto========"
#)
#add_custom_target(TargetPB ALL DEPENDS ${PROTO_CPP_FILES}) #你的具体项目里，再 设置依赖 例如 ADD_DEPENDENCIES(zone svr_util TargetPB)
###################################################

ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(test)
ADD_SUBDIRECTORY(test_single_process)
ADD_SUBDIRECTORY(samples/gameSvr)

#自定义命令
##################################
#ADD_CUSTOM_COMMAND(
#     OUTPUT f1
#     #DEPENDS f2 f3 f4*
#	 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
#     COMMAND sh build_svn_version.sh
#     COMMENT "out put build_svn_version"
#           )
#
#ADD_CUSTOM_TARGET(t2 ALL DEPENDS f1 o1)
# #ADD_DEPENDENCIES(server t2)

# add_custom_command(
     # OUTPUT o1
	 # WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
     # DEPENDS  ${PROJECT_BINARY_DIR}/../f2  ${PROJECT_BINARY_DIR}/../f4*  #文件的相对路径是 ${PROJECT_SOURCE_DIR}，
     # COMMAND cat ../f2 > o1
	 # COMMAND echo ${PROJECT_BINARY_DIR}
     # COMMENT "build main OUTPUT "
           # )

# ADD_CUSTOM_TARGET(t1 ALL DEPENDS o1)
# ADD_DEPENDENCIES(server t1)
##################################
